package com.start_eternity.oasissafewalk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.start_eternity.oasissafewalk.config.OrderNoGenerator;
import com.start_eternity.oasissafewalk.mapper.OrderMapper;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.Order;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.pojo.dto.OrderDTO;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.OrderService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * author:ZhangT
 * Date:2024 / 03 / 29 / 23:06
 * Description:
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OrderNoGenerator orderNoGenerator;

    //分页查询
    @Override
    public PageBean queryPage(int page, int pageSize) {

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<OrderDTO> orders = orderMapper.queryPage(page, pageSize);
        Page<OrderDTO> p = (Page<OrderDTO>) orders;
        //3.封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    //添加
    @Override
    public int addOrder(Order order) {

        order.setOrderNo(orderNoGenerator.generateOrderNo());
        order.setCtime(LocalDateTime.now());
        int i = orderMapper.insertOrder(order);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行插入订单,插入的订单为:" + order);
        loggService.insert(logg);
        return i;
    }

    //根据orderId修改
    @Override
    public int changeOrderById(Order order) {
        int i = orderMapper.updateOrderById(order);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据orderId修改订单,修改订单内容为:" + order);
        loggService.insert(logg);
        return i;
    }
    //根据orderNo修改
    @Override
    public int changeOrderByNo(Order order) {
        int i = orderMapper.updateOrderByNo(order);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据orderNo修改订单,修改订单内容为:" + order);
        loggService.insert(logg);
        return i;
    }
    //根据orderNo查询订单
    @Override
    public Order queryOrderByNo(String orderNo) {
        Order order = orderMapper.queryOrderByNo(orderNo);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据" + orderNo + "查询订单");
        loggService.insert(logg);

        return order;
    }

    //根据orderId查询订单
    @Override
    public Order queryOrderById(Integer orderId) {
        Order order = orderMapper.queryOrderById(orderId);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据" + orderId + "查询订单");
        loggService.insert(logg);

        return order;
    }

    //根据orderNo 链表查询订单详情
    @Override
    public Order getDetailsByNo(String orderNo) {
        Order details = orderMapper.getDetails(orderNo);
        return details;
    }

    @Override    public Order getParentByNo(String orderNo) {
        Order order = orderMapper.getParent(orderNo);
        return order;
    }

    @Override
    public OrderDTO getOrder(String orderNo) {
        OrderDTO info = orderMapper.getInfo(orderNo);
        return info;
    }
}
